package com.apes.hr.salary.rewardPunish.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.hr.base.model.WorkPost;
import com.apes.hr.base.repository.WorkPostRepository;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apestech.framework.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：奖惩类型
 * @author HuJianlin
 * @create 2018-05-05 15:11
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = RewardPunishType.CONST_TABLE_NAME)
public class RewardPunishType extends Model {
    public static final String CONST_TABLE_NAME = "hr_reward_punish_type";


    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "奖惩类型", padding = 4)
    @Comment("编码")
    private String id;   //奖惩类型编码

    @Name
    @Column(name = "quota",nullable = false , length = 4000)
    @Comment("指标")
    private String quota; //指标

    @Column(name = "file_name",  length = 1000)
    @Comment("文件名")
    private String filename; //文件名

    @Column(name = "instructions",  length = 1000)
    @Comment("其他说明")
    private String instructions; //其他说明

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "quota_type", referencedColumnName = "id" )
    @Comment("指标类别")
    private Enumeration quotaType;//指标类别

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "on_duty_state", referencedColumnName = "id")
    private Enumeration onDutyState; //在职状态(onDutyState)

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category", referencedColumnName = "id" )
    @Comment("奖惩类别")
    private Enumeration category; //奖惩类别 [枚举类型：rewardPunishCategory] (Reward: 奖励；Punish：处罚)

    @Column(name = "value",nullable = false,columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("单次分值")
    private int value;//单次分值

    @Column(name = "amt",nullable = false,columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("单次金额")
    private double amt;//单次金额

    @Column(name = "limit_value_u")
    @Comment("分值上限")
    private int limitValueU;//分值上限

    @Column(name = "limit_amt_u")
    @Comment("金额上限")
    private double limitAmtU;//金额上限

    @Column(name = "limit_value_l")
    @Comment("分值下限")
    private int limitValueL;//分值下限

    @Column(name = "limit_amt_l")
    @Comment("金额下限")
    private double limitAmtL;//金额下限



//    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
//    @Comment("有效标记")
//    private boolean valid = true; //有效

    @Column(name = "validity_date")
    @Comment("有效期起")
    private Date validityDate;//有效期起

    @Column(name = "validity_end_date")
    @Comment("有效期止")
    private Date validityEndDate;//有效期止

    @Column(name = "syn_interface")
    @Comment("外部接口名称")
    private String synInterface ;

    @Column(name = "posts_id", length = 4000)
    @Comment("岗位")
    private String postList;  //岗位

    public void setPostLists(String posts) {
        this.postList = posts;
    }

    @Transient
    private Set<WorkPost> postSet;

    public Set<WorkPost> getpostSet() {
        if (isWrite() || isLoad()) {
            postSet = getPost(this.postList);
        }
        return postSet;
    }
    public void setpostSet(Set<WorkPost> postSet) {
        this.postSet = postSet;
        if (!isWrite()) {
            this.postList = toConvertPost(this.postSet);
        }
    }

    @Transient
    private Set<WorkPost> getPost(String source) {
        if (StringUtil.isEmpty(source)) return null;
        Set<WorkPost> persons = new HashSet<>();
        String[] rows = source.split(";");
        for (String person : rows) {
            if (person.contains("|")) {
                persons.add(SpringManager.getBean(WorkPostRepository.class).findOne(StringUtils.split(person, "|")[0]));
            }
        }
        return persons;
    }

    private String toConvertPost(Set<WorkPost> persons) {
        StringBuffer sb = new StringBuffer();
        for (WorkPost workPost : persons) {
            if (sb.length() > 0) {
                sb.append(";");
            }
            sb.append(workPost.getId());
            sb.append("|");
            sb.append(workPost.getName());  // 格式 id|description;
        }
        return sb.toString();
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid; //审批人

    @Column(name = "approve_date")
    @Comment("审批日期")
    private Date approveDate; //审批日期

    @Column(name = "state", length = 10)
    @Comment("状态")
    private String state; //状态： ('sent', '提交'), ('done', '已审核')

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    private Dept dept; // 部门(用于部门只能查询本部门数据)

}
